package com.qg.knife4j;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.qg.knife4j.properties.Knife4jProperties;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.math.BigDecimal;

/**
 * @Author QG
 * @Date 2023/6/2 14:27
 * @description
 **/
//@Configuration
@EnableSwagger2
@EnableKnife4j
@EnableConfigurationProperties(Knife4jProperties.class)
@ConditionalOnProperty(value = "system.knife4j.enable",havingValue = "true",matchIfMissing = false)
public class Knife4jAutoConfiguration {

    @Autowired
    private Knife4jProperties knife4jProperties;

    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2)
                // 配置分组名
                .groupName(knife4jProperties.getGroupName())
                .apiInfo(apiInfo())
                .select()
                // 设置扫描包的地址 : com.hanliy.controller
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                // 设置路径筛选 只扫描com.hanliy.controller/test/下面的包
                // .paths(PathSelectors.ant("/test/**"))
                // com.hanliy.controller下的任何接口信息
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(knife4jProperties.getTitle())
                .description(knife4jProperties.getDescription())
                .termsOfServiceUrl(knife4jProperties.getServiceUrl())
                .version(knife4jProperties.getVersion())
                .build();
    }

}
